/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package redis;

/**
 *
 * @author dell
 */
import redis.clients.jedis.Jedis;
public class RedisUtil 
{
private static final String host="127.0.0.1";
private static final String password="123456";
/**
 * return a active Jedis instance 
 * @return 
 * @pass
 */
public static Jedis getJedis()
{
     Jedis jedis=null;
     try
     {
         jedis=new Jedis(host);
         jedis.auth(password);
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     return jedis;
}
/**
 * flush all datas in Redis
 * @pass
 */
public static void flushAll()
{
     Jedis jedis=getJedis();
     try
     {
         jedis.flushAll();
         jedis.close();
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
     finally 
     {
         jedis.close();
     }
}
/**
 * flush all datas in specified database
 * @param index 
 * @pass
 */
public static void flushDB(int index)
{
     Jedis jedis=getJedis();
     try
     {
         jedis.select(index);
         jedis.flushDB();
     }
     catch(Exception e)
     {
         e.printStackTrace();
     }
}
/**
 * return the size of the specified database
 * @param index
 * @return 
 * @pass
 */
public static long getDBsize(int index)
{
     long size=0;
     Jedis jedis=null;
     try
     {
         jedis=getJedis();
         jedis.select(index);
         size=jedis.dbSize();
     }
     catch(Exception e)       
     { 
         e.printStackTrace();
     }
     finally
     {
          jedis.close();
     }
     return size;
}
}
